package com.dqx.haoqiu.mvp.module.user;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BasePresenter;
import com.dqx.haoqiu.repository.repository.news.NewsRepository;
import com.dqx.haoqiu.repository.repository.user.UserRepository;
import com.xmjs.baselibrary.network.retrofit.RxUtil;

import javax.inject.Inject;

public class UserPresenter extends BasePresenter<UserContract.View> implements UserContract.Presenter {
    UserRepository userRepository;
    NewsRepository newsRepository;

    @Inject
    public UserPresenter(UserRepository userRepository, NewsRepository newsRepository) {
        this.userRepository = userRepository;
        this.newsRepository = newsRepository;
    }

    @Override
    public void getUserInfo() {
        userRepository.getUserInfo()
                .compose(RxUtil.applySchedulers())
                .compose(RxUtil.bindToLifecycle(view))
                .doOnSubscribe(disposable -> view.showLoadingView(true))
                .doAfterTerminate(() -> view.hideLoadingView())
                .subscribe(entity -> {
                    view.showUserInfo(entity);
                }, ex -> {
                    showNetworkError(ex, R.string.def_error_message, view);
                });
    }

    @Override
    public void getNews(int channel, int page, int page_size, long currentTime, int category) {
        newsRepository.getNews(channel, page, page_size, currentTime, category)
                .compose(RxUtil.applySchedulers())
                .compose(RxUtil.bindToLifecycle(view))
                .doOnSubscribe(disposable -> view.showLoadingView(true))
                .doAfterTerminate(() -> view.hideLoadingView())
                .subscribe(entities -> {
                    view.showNews(entities);
                }, ex -> {
                    showNetworkError(ex, R.string.def_error_message, view);
                });
    }
}